home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / utils / fbm2fl03.lha / fppframe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-05  |  3.9 KB  |  164 lines

  1. /****************************************************************
  2.  * fliframe.c
  3.  ****************************************************************/
  4.  
  5. /******
  6.   Copyright (C) 1993 by Klaus Ehrenfried. 
  7.  
  8.   Permission to use, copy, modify, and distribute this software
  9.   is hereby granted, provided that the above copyright notice appears 
  10.   in all copies and that the software is available to all free of charge. 
  11.   The author disclaims all warranties with regard to this software, 
  12.   including all implied warranties of merchant-ability and fitness. 
  13.   The code is simply distributed as it is.
  14. *******/
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <memory.h>
  19. #include "fpfli.h"
  20.  
  21. static unsigned char frame_header[FLI_FRAME_HEADER_SIZE];
  22.  
  23. #define ERRMSG "Image has wrong format !\n"
  24.  
  25. /****************************************************************
  26.  * fli_write_frame
  27.  ****************************************************************/
  28.  
  29. int
  30. fli_write_frame
  31. (
  32. UBYTE *prepre_pixel,
  33. UBYTE *pre_pixel,
  34. UBYTE *curr_pixel,
  35. LONG curr_color[],
  36. int first_flag
  37. )
  38. {
  39.     int chunks, size_color, size_pixel, help;
  40.     int frame_size;
  41.  
  42.     chunks=0;
  43.     /* fprintf(stdout," Making color chunk .....\n"); */
  44.     size_color=make_color_chunk(curr_color, first_flag);
  45.  
  46.     if (first_flag == 1)        /* if first frame */
  47.     {
  48.         fprintf(stdout," First frame !\n");
  49.     if (size_color == 0)        /* error occured */
  50.     {
  51.         fprintf(stderr,"Error: no colors in first frame\n");
  52.         return(-1);
  53.     }
  54.  
  55.         /* fprintf(stdout," Making brun chunk .....\n"); */
  56.     size_pixel=make_brun_chunk(curr_pixel);
  57.  
  58.     if (size_pixel == 0)        /* error occured */
  59.     {
  60.         fprintf(stderr,"Error: no pixel in first frame\n");
  61.         return(-1);
  62.     }
  63.     chunks=2;
  64.     }
  65.     else
  66.     {
  67.     if (size_color > 0)
  68.         chunks++;
  69.  
  70.         /* fprintf(stdout," Making delta chunk .....\n"); */
  71.     if (old_format_flag == 1)
  72.       size_pixel=make_lc_chunk(prepre_pixel, pre_pixel, curr_pixel);
  73.     else
  74.       size_pixel=make_delta_chunk(prepre_pixel, pre_pixel, curr_pixel);
  75.  
  76.     if (size_pixel > 0)
  77.         chunks++;
  78.     else
  79.         fprintf(stdout," No new pixels\n");
  80.     }
  81.  
  82.     frame_size=FLI_FRAME_HEADER_SIZE+size_color+size_pixel;
  83.  
  84.     help=0;
  85.     add_bytes(frame_header, &help, frame_size, IOM_LONG);
  86.     add_bytes(frame_header, &help, FLI_FRAME_MAGIC, IOM_UWORD);
  87.     add_bytes(frame_header, &help, chunks, IOM_UWORD);
  88.     add_bytes(frame_header, &help, 0x0000, IOM_LONG);
  89.     add_bytes(frame_header, &help, 0x0000, IOM_LONG);
  90.  
  91.     if (fwrite(frame_header, FLI_FRAME_HEADER_SIZE, 1, output) != 1)
  92.     return(-1);
  93.  
  94.     if (size_color > 0)
  95.     {
  96.     if (fwrite(color_chunk_buffer, size_color, 1, output) != 1)
  97.         return(-1);
  98.     }
  99.  
  100.     if (size_pixel > 0)
  101.     {
  102.     if (fwrite(pixel_chunk_buffer, size_pixel, 1, output) != 1)
  103.         return(-1);
  104.     }
  105.  
  106.     return(frame_size);
  107. }
  108.  
  109. /****************************************************************
  110.  * add_bytes
  111.  ****************************************************************/
  112.  
  113. void add_bytes(unsigned char record[], int *ipos, int value, int mode)
  114. {
  115.     int help;
  116.  
  117.     switch (mode)
  118.     {
  119.         case IOM_UBYTE:
  120.         record[(*ipos)++]=value;
  121.         break;
  122.  
  123.         case IOM_SBYTE:
  124.         value=(value >= 0) ? value : (256 + value);
  125.         value=(value >= 0) ? value : 0;
  126.         record[(*ipos)++]=value;
  127.         break;
  128.  
  129.         case IOM_UWORD:
  130.         help=value % 256;
  131.         record[(*ipos)++]=help;
  132.         help=(value - help)/256;
  133.         record[(*ipos)++]=help;
  134.         break;
  135.  
  136.         case IOM_SWORD:
  137.         value=(value >= 0) ? value : (65536 + value);
  138.         value=(value >= 0) ? value : 0;
  139.         help=value % 256;
  140.         record[(*ipos)++]=help;
  141.         help=(value - help)/256;
  142.         record[(*ipos)++]=help;
  143.         break;
  144.  
  145.         case IOM_LONG:
  146.         value=(value >= 0) ? value : 0;
  147.         help=value % 256;
  148.         record[(*ipos)++]=help;
  149.         value=(value - help)/256;
  150.  
  151.         help=value % 256;
  152.         record[(*ipos)++]=help;
  153.         value=(value - help)/256;
  154.  
  155.         help=value % 256;
  156.         record[(*ipos)++]=help;
  157.         value=(value - help)/256;
  158.  
  159.         help=value % 256;
  160.         record[(*ipos)++]=help;
  161.         break;
  162.     }
  163. }
  164.